package com.wmm.jinhong;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.common.dialog.MyDialog;
import com.common.net.mywidget.LoadingDialog;
import com.common.util.SPUtils;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class HistoryAdapter extends BaseAdapter {
    private Context context;
    private List<Bean> lists;
        private LayoutInflater inflater;
    private FragmentManager manager;
    LoadingDialog dialog;
    public HistoryAdapter(Context context, List<Bean> list, FragmentManager manager) {
        this.context = context;
        this.lists = list;
        this.manager=manager;
        dialog = new LoadingDialog(context);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Hodler hodler;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_his, null);
            hodler = new Hodler();
            hodler.button = convertView.findViewById(R.id.button);
            hodler.time = convertView.findViewById(R.id.time);
            hodler.type = convertView.findViewById(R.id.type);
            convertView.setTag(hodler);
        } else {
            hodler = (Hodler) convertView.getTag();
        }
        final Bean bean = lists.get(position);
        switch (bean.type) {
            case "1":
                hodler.type.setText("生产入库");
                break;
            case "2":
                hodler.type.setText("生产出库");
                break;
            case "3":
                hodler.type.setText("回厂确认");
                break;
            case "4":
                hodler.type.setText("空瓶采购");
                break;
            case "5":
                hodler.type.setText("采购入库");
                break;
            case "6":
                hodler.type.setText("自提出库");
                break;
            case "7":
                hodler.type.setText("自提入库");
                break;

        }
        hodler.time.setText(bean.time);
        hodler.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONArray jsonArray=null;
                String temp="";
                final List<GangPingBean> gangPingBeanList=new ArrayList<>();
                try {
                    jsonArray=new JSONArray(bean.list);
                    if(jsonArray!=null){
                        for (int i=0;i<jsonArray.length();i++){
                            gangPingBeanList.add(new Gson().fromJson(jsonArray.getJSONObject(i).toString(),GangPingBean.class));
                            temp+=jsonArray.getJSONObject(i).getString("GangPingHao");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyDialog.showMaterialDialog(manager, bean.ydhao+"钢瓶列表",temp, "删除记录", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                "time = ?", new String[]{bean.time});
                        lists.remove(position);
                        notifyDataSetChanged();
                    }
                }, "重新提交", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (bean.type) {
                            case "1":
                                firstSubmit(gangPingBeanList,position,bean.time);
                                break;
                            case "2":
                                scck(gangPingBeanList,bean.ydhao,position,bean.time);
                                break;
                            case "3":
                                hcqr(gangPingBeanList,bean.ydhao,position,bean.time);
                                break;
                            case "4":
                                kpcg(gangPingBeanList,bean.ydhao,position,bean.time);
                                break;
                            case "5":
                                cgrk(gangPingBeanList,bean.ydhao,position,bean.time);
                                break;
                            case "6":
                                ztck(gangPingBeanList,bean.ydhao,position,bean.time);
                                break;
                            case "7":
                                ztrk(gangPingBeanList,position,bean.time);
                                break;

                        }
                    }
                }).show();
            }
        });
        return convertView;
    }
    private void ztrk(List<GangPingBean> list, final int position, final String time) {
        JSONArray array = null;
        try {
            array = new JSONArray();
            JSONObject object;
            for (int i = 0; i < list.size(); i++) {
                object = new JSONObject();
                object.put("钢瓶号", list.get(i).getGangPingHao());
                object.put("介质", list.get(i).getInvName());
                object.put("规格", list.get(i).getcInvStd());
                object.put("物料编码", list.get(i).getcInvCode());
                object.put("充前重量", "");
                object.put("充后重量", "");
                array.put(object);
            }
            RequestParams requestParams = new RequestParams();
            requestParams.put("sJson", array.toString().replace("=", ":"));
            requestParams.put("sType", 8);
            requestParams.put("sStorageScanID", SPUtils.get(context, "UserName", "").toString());
            requestParams.put("sSyu", SPUtils.get(context, "YuInfo", "").toString());
            requestParams.put("isFull", 1);
            requestParams.put("yunDanHao", "");
            HttpUtils.post(Constant.SCRKURL, requestParams, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                }
                @Override
                public void onStart() {
                    super.onStart();
                    dialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    dialog.dismiss();
                }
                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    if (responseString.contains("true")) {
                        T.showShort(context, "入库成功");
                        MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                "time = ?", new String[]{time});
                        lists.remove(position);
                        notifyDataSetChanged();
                    } else {
                        T.showShort(context, "入库失败");
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void ztck(List<GangPingBean> list,String ydhao ,final int position, final String time){
        JSONArray array=null;
        try {
            array=new JSONArray();
            JSONObject object;
            for(int i=0;i<list.size();i++){
                object=new JSONObject();
                object.put("钢瓶号",list.get(i).getGangPingHao());
                object.put("介质",list.get(i).getInvName());
                object.put("规格",list.get(i).getcInvStd());
                object.put("物料编码",list.get(i).getcInvCode());
                object.put("充前重量","");
                object.put("充后重量","");
                array.put(object);
            }
            RequestParams requestParams=new RequestParams();
            requestParams.put("sJson",array.toString().replace("=",":"));
            requestParams.put("sFaYunDanHao",ydhao);
            requestParams.put("sStorageScanID",SPUtils.get(context,"UserName","").toString());
            requestParams.put("sSyu",SPUtils.get(context,"YuInfo","").toString());
            requestParams.put("sType",8);
            requestParams.put("sTiSongHuoDanHao","");
            HttpUtils.post(Constant.KPCGURL, requestParams, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                }

                @Override
                public void onStart() {
                    super.onStart();
                    dialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    dialog.dismiss();
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    if(responseString.contains("true")){
                        T.showShort(context,"出库成功");
                        MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                "time = ?", new String[]{time});
                        lists.remove(position);
                        notifyDataSetChanged();
                    }else{
                        T.showShort(context,"出库失败");
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void cgrk(List<GangPingBean> list,String ydhao,final int position, final String time){
        JSONArray array=null;
        try {
            array=new JSONArray();
            JSONObject object;
            for(int i=0;i<list.size();i++){
                object=new JSONObject();
                object.put("gangpinghao",list.get(i).getGangPingHao());
                object.put("isfull",list.get(i).getIsFull());
                object.put("backConfirm",list.get(i).getBackConfirm());
                array.put(object);
            }
            RequestParams requestParams=new RequestParams();
            requestParams.put("sJson",array.toString().replace("=",":"));
            requestParams.put("yunDanHao",ydhao);
            requestParams.put("sStorageScanID",SPUtils.get(context,"UserName","").toString());
            requestParams.put("sSyu",SPUtils.get(context,"YuInfo","").toString());
            requestParams.put("sType","6");
            requestParams.put("isFull",1);
            HttpUtils.post(Constant.SCRKURL, requestParams, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                }

                @Override
                public void onStart() {
                    super.onStart();
                    dialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    dialog.dismiss();
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    if(responseString.contains("true")){
                        T.showShort(context,"入库成功");
                        MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                "time = ?", new String[]{time});
                        lists.remove(position);
                        notifyDataSetChanged();
                    }else{
                        T.showShort(context,"入库失败");
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void kpcg(List<GangPingBean> list,String ydhao,final int position, final String time){
        JSONArray array=null;
        try {
            array=new JSONArray();
            JSONObject object;
            for(int i=0;i<list.size();i++){
                object=new JSONObject();
                object.put("钢瓶号",list.get(i).getGangPingHao());
                object.put("介质",list.get(i).getInvName());
                object.put("规格",list.get(i).getcInvStd());
                object.put("物料编码",list.get(i).getcInvCode());
                object.put("充前重量","");
                object.put("充后重量","");
                array.put(object);
            }
            RequestParams requestParams=new RequestParams();
            requestParams.put("sJson",array.toString().replace("=",":"));
            requestParams.put("sFaYunDanHao",ydhao);
            requestParams.put("sStorageScanID",SPUtils.get(context,"UserName","").toString());
            requestParams.put("sSyu",SPUtils.get(context,"YuInfo","").toString());
            requestParams.put("sType",6);
            requestParams.put("sTiSongHuoDanHao","");
            HttpUtils.post(Constant.KPCGURL, requestParams, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                }

                @Override
                public void onStart() {
                    super.onStart();
                    dialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    dialog.dismiss();
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    if(responseString.contains("true")){
                        T.showShort(context,"出库成功");
                        MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                "time = ?", new String[]{time});
                        lists.remove(position);
                        notifyDataSetChanged();
                    }else{
                        T.showShort(context,"出库失败");
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void hcqr(List<GangPingBean> list,String ydhao,final int position, final String time) {
        JSONArray array = null;
        try {
            array = new JSONArray();
            JSONObject object;
            for (int i = 0; i < list.size(); i++) {
                object = new JSONObject();
                object.put("gangpinghao", list.get(i).getGangPingHao());
                object.put("isfull", list.get(i).getIsFull());
                object.put("backConfirm", list.get(i).getBackConfirm());
                array.put(object);
            }
            RequestParams requestParams = new RequestParams();
            requestParams.put("sJson", array.toString().replace("=", ":"));
            requestParams.put("yunDanHao", ydhao);
            requestParams.put("sStorageScanID", SPUtils.get(context, "UserName", "").toString());
            requestParams.put("sSyu", SPUtils.get(context, "YuInfo", "").toString());
            requestParams.put("sType", "2");
            requestParams.put("isFull", 0);
            HttpUtils.post(Constant.SCRKURL, requestParams, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                }

                @Override
                public void onStart() {
                    super.onStart();
                    dialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    dialog.dismiss();
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    if (responseString.contains("true")) {
                        T.showShort(context, "回收成功");
                        MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                "time = ?", new String[]{time});
                        lists.remove(position);
                        notifyDataSetChanged();
                    } else {
                        T.showShort(context, "回收失败");
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void firstSubmit(final List<GangPingBean> list,final int position, final String time){
        JSONArray array=null;
        try {
            array=new JSONArray();
            JSONObject object;
            for(int i=0;i<list.size();i++){
                object=new JSONObject();
                object.put("钢瓶号",list.get(i).getGangPingHao());
                object.put("介质",list.get(i).getInvName());
                object.put("规格",list.get(i).getcInvStd());
                object.put("物料编码",list.get(i).getcInvCode());
                object.put("充前重量","");
                object.put("充后重量","");
                array.put(object);
            }
            RequestParams requestParams=new RequestParams();
            requestParams.put("sJson",array.toString().replace("=",":"));
            requestParams.put("sType","33");
            requestParams.put("sStorageScanID",SPUtils.get(context,"UserName","").toString());
            requestParams.put("sSyu",SPUtils.get(context,"YuInfo","").toString());
            requestParams.put("isFull",0);
            requestParams.put("yunDanHao","");
            HttpUtils.post(Constant.SCRKURL, requestParams, new TextHttpResponseHandler() {
                @Override
                public void onStart() {
                    super.onStart();
                    dialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    dialog.dismiss();
                }
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    if(responseString.contains("true")){
                        secondSubmit(list,position,time);
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void secondSubmit(List<GangPingBean> list,final int position, final String time){
        JSONArray array=null;
        try {
            array=new JSONArray();
            JSONObject object;
            for(int i=0;i<list.size();i++){
                object=new JSONObject();
                object.put("钢瓶号",list.get(i).getGangPingHao());
                object.put("介质",list.get(i).getInvName());
                object.put("规格",list.get(i).getcInvStd());
                object.put("物料编码",list.get(i).getcInvCode());
                object.put("充前重量","");
                object.put("充后重量","");
                array.put(object);
            }
            RequestParams requestParams=new RequestParams();
            requestParams.put("sJson",array.toString().replace("=",":"));
            requestParams.put("sType","1");
            requestParams.put("sStorageScanID",SPUtils.get(context,"UserName","").toString());
            requestParams.put("sSyu",SPUtils.get(context,"YuInfo","").toString());
            requestParams.put("isFull",0);
            requestParams.put("yunDanHao","");
            HttpUtils.post(Constant.SCRKURL, requestParams, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                }
                @Override
                public void onStart() {
                    super.onStart();
                    dialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    dialog.dismiss();
                }
                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    if(responseString.contains("true")){
                        T.showShort(context,"领料成功！入库成功");
                        MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                "time = ?", new String[]{time});
                        lists.remove(position);
                        notifyDataSetChanged();
                    }else{
                        T.showShort(context,"领料成功！入库失败");

                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void scck(List<GangPingBean> list,String ydhao,final int position, final String time){
        JSONArray array=null;
        try {
            array=new JSONArray();
            JSONObject object;
            for(int i=0;i<list.size();i++){
                object=new JSONObject();
                object.put("钢瓶号",list.get(i).getGangPingHao());
                object.put("介质",list.get(i).getInvName());
                object.put("规格",list.get(i).getcInvStd());
                object.put("物料编码",list.get(i).getcInvCode());
                object.put("充前重量","");
                object.put("充后重量","");
                array.put(object);
            }
            RequestParams requestParams=new RequestParams();
            requestParams.put("sJson",array.toString().replace("=",":"));
            requestParams.put("sFaYunDanHao",ydhao);
            requestParams.put("sStorageScanID",SPUtils.get(context,"UserName","").toString());
            requestParams.put("sSyu",SPUtils.get(context,"YuInfo","").toString());
            requestParams.put("sTiSongHuoDanHao","");
            HttpUtils.post(Constant.SCCKURL, requestParams, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                }

                @Override
                public void onStart() {
                    super.onStart();
                    dialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    dialog.dismiss();
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    if(responseString.contains("true")){
                        T.showShort(context,"出库成功");
                        MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                "time = ?", new String[]{time});
                        lists.remove(position);
                        notifyDataSetChanged();
                    }else{
                        T.showShort(context,"出库失败");
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    class Hodler {
        TextView type;
        TextView time;
        Button button;
    }
}
